<?php
declare (strict_types=1);

namespace app\sysuser\controller\learn;

use app\common\controller\SysBackend;
use app\common\model\banxi\course\Course;
use app\common\model\banxi\learn\LearnVocabularyRecord as LearnVocabularyRecordModel;
use app\common\model\banxi\organization\Organization as OrganizationModel;
use app\common\service\AccountService;
use ba\Exception;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ValidateException;

class LearnVocabularyRecord extends SysBackend
{
    protected array $noNeedLogin = [];
    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new LearnVocabularyRecordModel();
    }


    /**
     * 分页查询
     * @return void
     * @throws DbException
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);
        $param['query']['EQ_isSubmit'] = 1;
        //查询条件解析
        $where = get_query($param);
        //获取分页数据
        $pageParams = get_page_params($param);
        $list = $this->model->with([
            'settingInfo' => function ($query) {
                $query->withoutField('languageAbility,analysis,suggest');
            }])
            ->where($where)
            ->order(['id' => 'asc'])
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit'],
            ]);
        $list = result_row($list);
        $AccountService = new AccountService();
        $account = $AccountService->wrapperAccountInfo($list['rows']);
        $list['rows'] = $account;
        $this->ok('操作成功', $list);
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    // public function get()
    // {
    //     $param = $this->request->param(["query" => []]);
    //     if (empty($param['query'])) {
    //         $this->fail('请传递你要查询的记录');
    //     }
    //     $where = get_query($param);
    //     $info = $this->model->with([
    //         'studentInfo' => function ($query) {
    //             $query->field('id,name');
    //         }, 'trainerInfo' => function ($query) {
    //             $query->field('id,name');
    //         }, 'organizationBaseInfo' => function ($query) {
    //             $query->field('id,name,managerName');
    //         }, 'accountClassHour'])->where($where)->find();
    //     if (empty($info)) {
    //         $this->fail('记录不存在');
    //     }
    //     $info['actualCourseIdList'] = explode(',', $info['actualCourseId']);
    //     $info['actualCourseList'] = Course::where([['id', 'in', $info['actualCourseIdList']]])->field(['id', 'name'])->select();
    //     if (!$info['organizationBaseInfo']) {
    //         $organizationModel = new OrganizationModel();
    //         $info['organizationBaseInfo'] = $organizationModel->getOrganizationInfo();
    //     }
    //     $this->ok('操作成功', $info);
    // }

    /**
     * 查询-报告
     */
    public function getReport()
    {
        $param = $this->request->param(["recordId" => 0]);
        if (!$param['recordId']) {
            $this->fail('请选择你要生成报告的测试记录');
        }
        try {
            $detail = $this->model->getDetail($param['recordId']);
        } catch (Exception|PDOException|ValidateException $e) {
            $this->fail($e->getMessage());
        }
        $this->ok('操作成功', $detail);


    }
}
